﻿#ifndef SRC_CORE_EVENT_SERVICE_H_
#define SRC_CORE_EVENT_SERVICE_H_

#include "base/thread.h"
#include "core/async_listener.h"
#include "core/async_packet_socket.h"
#include <map>

namespace vz {

//class AsyncListener;
//typedef scoped_refptr<AsyncListener> AsyncListener::Ptr;

class EventService : public RefCountInterface {
 public:
  typedef scoped_refptr<EventService> Ptr;

  EventService(Thread* thread);
  virtual ~EventService();

  static Ptr GetCurrentEventService();
  static Ptr CreateEventService();

  void Post(MessageHandler *phandler, uint32 id = 0,
            MessageData *pdata = NULL, bool time_sensitive = false);

  void PostDelayed(int cmsDelay, MessageHandler *phandler,
                   uint32 id = 0, MessageData *pdata = NULL);

  void PostAt(uint32 tstamp, MessageHandler *phandler,
              uint32 id = 0, MessageData *pdata = NULL);

  AsyncListener::Ptr CreateListener();

  AsyncPacketSocket::Ptr CreateSocket();

  void Run();

  void Quit();

 private:
  Ptr SharedFromThis();

 private:
  scoped_ptr<Thread> thread_;
  static std::map<Thread*, EventService::Ptr> event_services_;
};

}  // namespace vz

#endif  // SRC_CORE_EVENT_SERVICE_H_
